package com.ruoyi.project.sdi.sdiPaeaArc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.CommonParaTool;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sdi.sdiImage.domain.SdiImage;
import com.ruoyi.project.sdi.sdiImage.service.ISdiImageService;
import com.ruoyi.project.sdi.sdiPaeaArc.domain.SdiPaeaArc;
import com.ruoyi.project.sdi.sdiPaeaArc.enums.ArchivePaeaStatus;
import com.ruoyi.project.sdi.sdiPaeaArc.model.SdiPaeaArcQueryRequest;
import com.ruoyi.project.sdi.sdiPaeaArc.service.ISdiPaeaArcService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.domain.UserStatus;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 扫描批次Controller
 *
 * @author 关晓强
 * @date 2020-03-16
 */
@Controller
@RequestMapping("/sdi/sdiPaeaArc")
public class SdiPaeaArcController extends BaseController
{
    private String prefix = "sdi/sdiPaeaArc";

    @Autowired
    private ISdiPaeaArcService sdiPaeaArcService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ISdiImageService sdiImageService;

    @RequiresPermissions("sdi:sdiPaeaArc:view")
    @GetMapping()
    public String sdiPaeaArc()
    {
        return prefix + "/sdiPaeaArc";
    }

    /**
     * 查询扫描批次列表
     */
    @RequiresPermissions("sdi:sdiPaeaArc:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SdiPaeaArcQueryRequest sdiPaeaArcQueryRequest)
    {
        Date trDate = sdiPaeaArcQueryRequest.getTrDate();
        Date beginTrDate = sdiPaeaArcQueryRequest.getBeginTrDate();
        Date endTrDate = sdiPaeaArcQueryRequest.getEndTrDate();
        startPage();
        List<SdiPaeaArc> list = sdiPaeaArcService.list(new QueryWrapper<SdiPaeaArc>(sdiPaeaArcQueryRequest)
                .eq(trDate!=null,"tr_date",trDate)
                .ge(beginTrDate!=null,"tr_date", beginTrDate)
                .le(endTrDate!=null,"tr_date", endTrDate));
        return getDataTable(list);
    }

    /**
     * 导出扫描批次列表
     */
    @RequiresPermissions("sdi:sdiPaeaArc:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SdiPaeaArcQueryRequest sdiPaeaArcQueryRequest)
    {
        Date beginTrDate = sdiPaeaArcQueryRequest.getBeginTrDate();
        Date endTrDate = sdiPaeaArcQueryRequest.getEndTrDate();
        List<SdiPaeaArc> list = sdiPaeaArcService.list(new QueryWrapper<SdiPaeaArc>(sdiPaeaArcQueryRequest)
                .ge(beginTrDate!=null,"tr_date", beginTrDate)
                .le(endTrDate!=null,"tr_date", endTrDate));
        ExcelUtil<SdiPaeaArc> util = new ExcelUtil<SdiPaeaArc>(SdiPaeaArc.class);
        return util.exportExcel(list, "sdiPaeaArc");
    }

    /**
     * 新增扫描批次
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存扫描批次
     */
    @RequiresPermissions("sdi:sdiPaeaArc:add")
    @Log(title = "扫描批次", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SdiPaeaArc sdiPaeaArc)
    {
        return toAjax(sdiPaeaArcService.saveOrUpdate(sdiPaeaArc));
    }

    /**
     * 修改扫描批次
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        SdiPaeaArc sdiPaeaArc = sdiPaeaArcService.getById(id);
        mmap.put("sdiPaeaArc", sdiPaeaArc);
        return prefix + "/edit";
    }

    /**
     * 扫描批次详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        SdiPaeaArc sdiPaeaArc = sdiPaeaArcService.getById(id);
        List<SdiImage> sdiImageList = sdiImageService.getSdiImagesBySdiPaeaArc(sdiPaeaArc);
        String imgPathPrefix = "http://"
                + CommonParaTool.getImgPathPrefix(sdiPaeaArc.getAuditOrgCode());
        mmap.put("sdiPaeaArc", sdiPaeaArc);
        mmap.put("sdiImageList", sdiImageList);
        mmap.put("imgPathPrefix", imgPathPrefix);
        return prefix + "/detail";
    }

    /**
     * 修改保存扫描批次
     */
    @RequiresPermissions("sdi:sdiPaeaArc:edit")
    @Log(title = "扫描批次", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SdiPaeaArc sdiPaeaArc)
    {
        return toAjax(sdiPaeaArcService.saveOrUpdate(sdiPaeaArc));
    }

    /**
     * 删除扫描批次
     */
    @RequiresPermissions("sdi:sdiPaeaArc:remove")
    @Log(title = "扫描批次", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        this.sdiPaeaArcService.removeArc(ids);
        return AjaxResult.success();
    }

    /**
     *
     */
    /**
     * 检查可操作状态
     */
    @PostMapping( "/checkStatus")
    @ResponseBody
    public AjaxResult checkStatus(int status,String buttonType) {
        if("callBackToUser".equals(buttonType)){
            if(ArchivePaeaStatus.Complete.getValue() != status
                    && ArchivePaeaStatus.DataInputing.getValue() != status){
                return AjaxResult.warn("该批次非归档或正在补录，无法回退至指定人员！");
            }
        }
        return AjaxResult.success();
    }
    /**
     * 解锁批次
     */
    @RequiresPermissions("sdi:sdiPaeaArc:unlock")
    @Log(title = "扫描批次", businessType = BusinessType.UPDATE)
    @PostMapping("/unlock")
    @ResponseBody
    public AjaxResult unlock(SdiPaeaArc sdiPaeaArc) {
        sdiPaeaArc = sdiPaeaArcService.getById(sdiPaeaArc);
        int status = sdiPaeaArc.getStatus();
        //这些状态不允许解锁
        if (ArchivePaeaStatus.DataInputing.getValue() == status ||
                ArchivePaeaStatus.ForceInputing.getValue() == status ||
                ArchivePaeaStatus.Ocring.getValue() == status ||
                ArchivePaeaStatus.ThirdOcring.getValue() == status ||
                ArchivePaeaStatus.TradeMatching.getValue() == status ||
                ArchivePaeaStatus.正在重新补录795.getValue() == status ||
                ArchivePaeaStatus.Ocring.getValue() == status) {
                sdiPaeaArc.setStatus(ArchivePaeaStatus.toArcBatchStatus(
                        sdiPaeaArc.getStatus()).getUnlockStatus().getValue());
                return toAjax(sdiPaeaArcService.updateById(sdiPaeaArc));
        } else {
            return AjaxResult.warn("扫描批次当前状态不允许解锁");
        }
    }

    /**
     * 回退批次
     */
    @RequiresPermissions("sdi:sdiPaeaArc:callBack")
    @Log(title = "扫描批次", businessType = BusinessType.UPDATE)
    @PostMapping("/callBack")
    @ResponseBody
    public AjaxResult callBack(SdiPaeaArc sdiPaeaArc) {
        int status = sdiPaeaArc.getStatus();
        if(ArchivePaeaStatus.Deleted.getValue() == status){
            return AjaxResult.warn("该批次已删除,无法回退！");
        } else if (ArchivePaeaStatus.WaitScan.getValue() == status){
            return AjaxResult.warn("该批次未扫描状态，无需回退！");
        } else if (ArchivePaeaStatus.WaitOcr.getValue() == status){
            return AjaxResult.warn("该批次待OCR识别，无法回退！");
        } else if (ArchivePaeaStatus.WaitScan.getValue() == status){
            return AjaxResult.warn("该批次正在手工补录,无法回退，请先解锁再回退！");
        } else if (ArchivePaeaStatus.DataInputing.getValue() == status){
            return AjaxResult.warn("该批次正在重新手工补录,无法回退，请先解锁再回退！");
        } else if (ArchivePaeaStatus.正在重新补录795.getValue() == status){
            return AjaxResult.warn("该批次未扫描状态，无需回退！");
        } else if (ArchivePaeaStatus.ThirdOcring.getValue() == status){
            return AjaxResult.warn("该批次正在重新手工补录,无法回退，请先解锁再回退！");
        } else if (ArchivePaeaStatus.Ocring.getValue() == status){
            return AjaxResult.warn("该批次正在进行OCR识别,无法回退！");
        } else if (ArchivePaeaStatus.TradeMatching.getValue() == status){
            return AjaxResult.warn("该批次正在进行流水勾对，无法回退！");
        } else if (ArchivePaeaStatus.ForceInputing.getValue() == status){
            return AjaxResult.warn("该批次正在强制补录状态，无法回退，请先解锁再回退！");
        }
        sdiPaeaArcService.callBackSdiPaeaArc(sdiPaeaArc);
        return AjaxResult.success();
    }


    /**
     * 回退批次
     */
    @RequiresPermissions("sdi:sdiPaeaArc:callBackToUser")
    @Log(title = "扫描批次", businessType = BusinessType.UPDATE)
    @PostMapping("/callBackToUser")
    @ResponseBody
    public AjaxResult callBackToUser(SdiPaeaArcQueryRequest sdiPaeaArcQueryRequest) {
        sdiPaeaArcService.callBackSdiPaeaArcToUser(sdiPaeaArcQueryRequest);
        return AjaxResult.success();
    }

    /**
     * 重新补录批次
     */
    @RequiresPermissions("sdi:sdiPaeaArc:reInput")
    @Log(title = "扫描批次", businessType = BusinessType.UPDATE)
    @PostMapping("/reInput")
    @ResponseBody
    public AjaxResult reInput(SdiPaeaArc sdiPaeaArc) {
        sdiPaeaArc = sdiPaeaArcService.getById(sdiPaeaArc);
        int status = sdiPaeaArc.getStatus();
        String inputer = sdiPaeaArc.getInputer();
        if (ArchivePaeaStatus.Complete.getValue() == status) {
            User user = userService.selectUserByLoginName(inputer);

            if (user != null){
                String userStatus = user.getStatus();
                if(UserStatus.OK.getCode().equals(userStatus)){
                    sdiPaeaArc.setStatus(ArchivePaeaStatus.正在重新补录795.getValue());
                    sdiPaeaArc.setLocker(sdiPaeaArc.getInputer());
                }else{
                    sdiPaeaArc.setStatus(ArchivePaeaStatus.待重新补录790.getValue());
                    sdiPaeaArc.setLocker("-1");
                }
            }else{
                sdiPaeaArc.setStatus(ArchivePaeaStatus.待重新补录790.getValue());
                sdiPaeaArc.setLocker("-1");
            }
            sdiPaeaArc.setPreviousInputer(sdiPaeaArc.getInputer());
            return toAjax(sdiPaeaArcService.saveOrUpdate(sdiPaeaArc));
        } else {
            return AjaxResult.warn("扫描批次当前状态不允许重新补录");
        }
    }
}
